package com.koron.configure.workflow.service.impl;


import cn.hutool.core.util.StrUtil;
import com.koron.common.core.business.workflow.bean.po.ConfigureWorkflowStaff;
import com.koron.configure.workflow.mapper.ConfigureWorkflowPluginMapper;
import com.koron.configure.workflow.mapper.ConfigureWorkflowStaffMapper;
import com.koron.configure.workflow.service.ConfigureWorkflowStaffService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 业务配置管理-流程配置-人员(ConfigureWorkflowStaff)表服务实现类
 *
 * @author jzp
 * @since 2022-01-11 11:36:45
 */
@Service
public class ConfigureWorkflowStaffServiceImpl implements ConfigureWorkflowStaffService {

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public ConfigureWorkflowStaff getById(SessionFactory factory, String id) {
        ConfigureWorkflowStaffMapper mapper = factory.getMapper(ConfigureWorkflowStaffMapper.class);
        return mapper.selectById(id);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<ConfigureWorkflowStaff> list(SessionFactory factory, ConfigureWorkflowStaff configureWorkflowStaff) {
        ConfigureWorkflowStaffMapper mapper = factory.getMapper(ConfigureWorkflowStaffMapper.class);
        return mapper.selectList(configureWorkflowStaff);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("listByWorkflowId")
    public List<ConfigureWorkflowStaff> listByWorkflowId(SessionFactory factory, String workflowId) {
        if (StringUtils.isEmpty(workflowId)) {
            return new ArrayList<>();
        }
        ConfigureWorkflowStaff configureWorkflowStaff = new ConfigureWorkflowStaff();
        configureWorkflowStaff.setWorkflowId(workflowId);
        return this.list(factory, configureWorkflowStaff);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, ConfigureWorkflowStaff configureWorkflowStaff) {
        ConfigureWorkflowStaffMapper mapper = factory.getMapper(ConfigureWorkflowStaffMapper.class);
        return mapper.insertOrUpdate(configureWorkflowStaff) > 0;
    }

    @Override
    @TaskAnnotation("saveBatch")
    public boolean saveBatch(SessionFactory factory, List<ConfigureWorkflowStaff> configureWorkflowStaff) {
        if (CollectionUtils.isEmpty(configureWorkflowStaff)) {
            return false;
        }
        ConfigureWorkflowStaffMapper mapper = factory.getMapper(ConfigureWorkflowStaffMapper.class);
        return mapper.insertOrUpdateBatch(configureWorkflowStaff) > 0;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, ConfigureWorkflowStaff configureWorkflowStaff) {
        ConfigureWorkflowStaffMapper mapper = factory.getMapper(ConfigureWorkflowStaffMapper.class);
        return mapper.update(configureWorkflowStaff) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteBatchIds")
    public boolean deleteBatchIds(SessionFactory factory, List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        ConfigureWorkflowPluginMapper mapper = factory.getMapper(ConfigureWorkflowPluginMapper.class);
        return mapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @TaskAnnotation("removeByWorkflowId")
    public boolean removeByWorkflowId(SessionFactory factory, String workflowId){
        if (StrUtil.isEmpty(workflowId)) {
            return false;
        }
        ConfigureWorkflowStaff configureWorkflowStaff = new ConfigureWorkflowStaff();
        configureWorkflowStaff.setWorkflowId(workflowId);
        List<ConfigureWorkflowStaff> list = list(factory, configureWorkflowStaff);

        return deleteBatchIds(factory, list.stream().map(ConfigureWorkflowStaff::getId).collect(Collectors.toList()));
    }
}
